package com.jeeplus.test.jlnu.qualificationInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AdmissionInfo;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AllInfo;
import com.jeeplus.test.jlnu.admissionInfo.mapper.ExcelMapper;
import com.jeeplus.test.jlnu.archivalInfo.domain.entity.ArchParmerter;
import com.jeeplus.test.jlnu.fieldConfig.domain.FieldConfigEntity;
import com.jeeplus.test.jlnu.fieldConfig.mapper.QualificationFieldConfigMapper;
import com.jeeplus.test.jlnu.qualificationInfo.domain.entity.QualificationInfo;
import com.jeeplus.test.jlnu.qualificationInfo.mapper.QualificationInfoMapper;
import com.jeeplus.test.jlnu.qualificationInfo.service.QualificationInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QualificationInfoServiceImpl implements QualificationInfoService {

    @Autowired
    private QualificationInfoMapper qualificationInfoMapper;

    @Autowired
    private QualificationFieldConfigMapper qualificationFieldConfigMapper;

    @Autowired
    private ExcelMapper excelMapper;

    @Override
    public Page<QualificationInfo> getQualificationInfoPage(ArchParmerter qualificationInfoReqVo, Page<QualificationInfo> page) {
        // 1. 获取分页结果和字段配置信息
        Page<QualificationInfo> qualificationInfoPage = qualificationInfoMapper.getQualificationInfoPage(qualificationInfoReqVo, page);


        return qualificationInfoPage;
    }

    @Override
    public int updateQual(QualificationInfo qualificationInfo) {
      return  qualificationInfoMapper.updateQual(qualificationInfo);
    }

    @Override
    public int checkStudentId(String studentId) {
        return qualificationFieldConfigMapper.checkStudentId(studentId);
    }

    @Override
    public int addQualificationInfo(QualificationInfo info) {
        List<AdmissionInfo> admissionInfos = new ArrayList<>();
        AdmissionInfo admissionInfo = new AdmissionInfo();
        BeanUtils.copyProperties(info,admissionInfo);
        admissionInfo.setStudentId(info.getStudentId());
        admissionInfo.setExpectedGraduationDate(info.getGraduationDate());
        admissionInfos.add(admissionInfo);
        excelMapper.saveAdmission(admissionInfos);
        // 插入学籍信息表（只插入学号）
        excelMapper.saveSecondData(admissionInfos);
        // 插入毕业信息表（只插入学号）
//        excelMapper.saveThirdData(admissionInfos);
        // 插入就业信息表（只插入学号）
        excelMapper.saveFourth(admissionInfos);
        // 插入学位信息表（只插入学号）
        excelMapper.saveFifth(admissionInfos);
        // 插入学历信息表（只插入学号）
        excelMapper.saveSeventh(admissionInfos);
//        excelMapper.saveEighth(admissionInfos);

//        excelMapper.saveEighth(admissionInfos);
        excelMapper.saveFourteenth(admissionInfos);
        excelMapper.saveFifteenth(admissionInfos);
        // 创建两个列表来存储不同 flag 的对象
        List<AdmissionInfo> flagOneList = new ArrayList<>();
        List<AdmissionInfo> otherFlagList = new ArrayList<>();

        for (AdmissionInfo admissionInfoss : admissionInfos) {
            if ("1".equals(admissionInfoss.getFlag())) {
                flagOneList.add(admissionInfoss);
            } else {
                otherFlagList.add(admissionInfoss);
            }
        }

        // 对于 flag 为 1 的对象调用保存方法
        if (!flagOneList.isEmpty()) {
            excelMapper.saveNinth(flagOneList);
            excelMapper.saveTenth(flagOneList);
            excelMapper.saveEleth(flagOneList);
            excelMapper.saveThirteenth(flagOneList);
        }

        // 对于 flag 为 2 或其他的对象调用保存方法
        if (!otherFlagList.isEmpty()) {
            excelMapper.saveTwelfth(otherFlagList);
            excelMapper.saveSixteenth(otherFlagList);
            excelMapper.saveSeventeenth(otherFlagList);
            excelMapper.saveEighteenth(otherFlagList);
        }
        return qualificationFieldConfigMapper.saveQualification(info);
    }
}
